package com.gxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.mapper.GradeMapper;
import com.gxa.model.ResultData;
import com.gxa.pojo.Grade;
import com.gxa.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周区区
 * @version 1.0
 * @date 2021/8/3 17:18
 */
@Service
public class GradeServiceImpl implements GradeService {
    @Autowired
    private GradeMapper gradeMapper;

    /**
     * 查询所有班级信息
     * @return
     */
    @Override
    public ResultData findAllGrade() {
        //查询构造器
        QueryWrapper qw = new QueryWrapper();
        //key：数据库列名  value：数据库的值
        Map<String, Object> map = new HashMap<>();
        //把查询条件放到查询构造器中
        qw.allEq(map,false);
        //调用MP的查询方法
        List<Grade> allGrade = gradeMapper.selectList(qw);
        long count = gradeMapper.selectCount(qw);
        if(allGrade.size()>0){
            return ResultData.ok().data(allGrade).count(count).message("查找成功");
        }
        return ResultData.error().message("查找失败");
    }

    /**
     * 修改班级信息
     * @param grade
     * @return
     */
    @Override
    public ResultData updGrade(Grade grade) {
        int i = gradeMapper.updateById(grade);
        if(i>0){
            return ResultData.ok().message("修改成功");
        }
        return ResultData.error().message("修改失败");
    }

    /**
     * 根据id删除班级信息
     * @param gradeId
     * @return
     */
    @Override
    public ResultData delGrade(Integer gradeId) {
        int i = gradeMapper.deleteById(gradeId);
        if(i>0){
            return ResultData.ok().message("删除成功");
        }
        return ResultData.error().message("删除失败");
    }

    @Override
    public ResultData addGrade(Grade grade) {
        int i = gradeMapper.insert(grade);
        if(i>0){
            return ResultData.ok().message("添加成功");
        }
        return ResultData.error().message("添加失败");
    }
}
